//
//  YZMTabbar.swift
//  YZMweibo
//
//  Created by Simon on 16/4/18.
//  Copyright © 2016年 Simon. All rights reserved.
//

import UIKit
//声明协议
protocol YZMTabbarDelegate :NSObjectProtocol{
    // 协议方法
    func compbuttonClick()

}


class YZMTabbar: UITabBar {

    //声明代理
    weak var barDele:YZMTabbarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        
    }
    //重写 init frame 必须实现 init coder 方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //监听方法
    @objc private func compbuttonClick(){
        barDele?.compbuttonClick()
    
    }
    //设置控件
    func setUI(){
        
        addSubview(compButton)
        
    }
    //设置布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = ScreenW / 5
        
        //使用标识
        var index: CGFloat = 0
        
        for value in self.subviews{
            // 先判断他的子控件知否是我们的需要的UITabBarButton
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                // 设置UITabBarButton x
                value.frame.origin.x = index * width
                // 设置UITabBarButton 宽度
                value.frame.size.width = width
                
                // index ++
                index++
                
                // 判断index == 2  再++
                if index == 2{
                    index++
                }
            }
        }
        compButton.center.x = self.frame.width * 0.5
        compButton.center.y = self.frame.height * 0.5
        
    
    }
    
    //懒加载控件
    private lazy var compButton: UIButton = {
        let button = UIButton()
        
        //添加点击事件
        button.addTarget(self, action: "compbuttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        //背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        //设置Image
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.sizeToFit()
        
        return button
    }()
    
}
